const db=wx.cloud.database()
Page({
  data:{
    placelist:wx.getStorageSync('placelist'),
    pricelist:wx.getStorageSync('pricelist'),
    itemlist:wx.getStorageSync('itemlist'),
    clickitem:wx.getStorageSync('clickitem'),
    index: '0',
    picker: ['请选择人数','1', '2', '3', '4', '5'],
    phone: wx.getStorageSync('phone'),
    total:0,
    sum:0
  },
  onShow(){
    var total=this.data.total
    this.data.clickitem.forEach(element => {
      total+=parseInt(this.data.pricelist[element])
      this.setData({
        total,
        // sum:tota
      })
    });
  },
  getNowDate: function () {
    var date = new Date();
    var year = date.getFullYear() //年
    var month = date.getMonth() + 1 //月
    var day = date.getDate() //日
    var hour = date.getHours() //时
    var minute = date.getMinutes() //分
    var second = date.getSeconds() //秒
    var xiaoshi = "";
    if (hour < 10) {
      xiaoshi = "0" + hour;
    } else {
      xiaoshi = hour + "";
    }
    var fenzhong = "";
    if (minute < 10) {
      fenzhong = "0" + minute;
    } else {
      fenzhong = minute + "";
    }
    var miao = "";
    if (second < 10) {
      miao = "0" + second;
    } else {
      miao = second + "";
    }
    var time = year + '-' + month + '-' + day + ' ' + xiaoshi + ':' + fenzhong + ':' + miao
    return time
  },
  submit(e){
    // 这里应该怎么设计
    if (this.data.index=='0') {
      wx.showToast({
        title: '请先选择人数',
        icon:"none"
      })
      return
    }
    wx.showLoading({
      title: '正在操作',
    })

    db.collection('dingzhiList').add({
      data:{
        aprice:this.data.total+200,
        brief:'',
        placelist:this.data.clickitem,
        salenum:'1',
        loginId:wx.getStorageSync('loginId'),
        phone:wx.getStorageSync('phone'),
        time:this.getNowDate(),
        peoplenum:this.data.picker[this.data.index],
        total:this.data.sum,
        isuse:false
      }
    }).then(res=>{
      console.log(res)
      wx.hideLoading()
      wx.showToast({
        title: '支付成功',
      })
      setTimeout(() => {
        wx.reLaunch({
          url: '../index/index',
        })
      }, 1000);
    })

  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
    var num=parseInt(e.detail.value)
    var total=parseInt(this.data.total)+200
    this.setData({
      sum:num*total
    })
  },
  tolast(e){
    console.log(e)
    var price=e.currentTarget.dataset.price
    wx.navigateTo({
      url: '../tjdingzhipaylast/tjdingzhipaylast?price='+price,
    })
  }
})